<template>
  <div class="modal"  @click="handleBackgroundClick">
    <div class="modal-content" @click.stop>
      <div class="feedback-container">
        <h3>所有菜品反馈</h3>
        <div class="feedback-list">
          <table class="feedback-table" v-if="feedbacks.length">
            <thead>
            <tr>
              <th>菜品名称</th>
              <th>反馈内容</th>
              <th>提交日期</th>
            </tr>
            </thead>
            <tbody>
            <tr v-for="feedback in feedbacksWithDishName" :key="feedback.feedback_id">
              <td>{{ feedback.dishName }}</td>
              <td class="feedback-content">{{ feedback.content }}</td>
              <td>{{ feedback.date }}</td>
            </tr>
            </tbody>
          </table>
          <p v-if="!feedbacks.length">暂无反馈。</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getAllFeedbacks, getAllDishesEvenDeleted } from '@/api/dishes'; // 假设这是获取所有反馈的 API 函数

export default {
  name: 'DishAllFeedbackCard',

  props: {
    dishes: {
      type: Array,
      required: true
    },
    dish: {
      type: Object,
      required: false
    }
  },
  data() {
    return {
      feedbacks: [], // 存储所有菜品的反馈信息
      dishes: []
    };
  },

  computed: {
    feedbacksWithDishName() {
      return this.feedbacks.map(feedback => {
        const dish = this.dishes.find(d => d.dish_id === feedback.dish_id);
        return {
          ...feedback,
          dishName: dish ? dish.name : '已删除菜品'
        };
      });
    }
  },

  mounted() {
    this.fetchAllDishesEvenDeleted();
    this.fetchAllFeedbacks();

    document.addEventListener('keydown', this.handleKeydown);
  },
  beforeDestroy() {
    document.removeEventListener('keydown', this.handleKeydown);
  },

  methods: {
    close() {
      this.$emit('close');
    },
    handleKeydown(e) {
      if (e.key === 'Escape') {
        this.close();
      }
    },
    handleBackgroundClick() {
      this.$emit('close');
    },

    fetchAllFeedbacks() {

      getAllFeedbacks() // 调用 API 获取所有反馈
          .then(response => {
            if (response && response.data) {
              this.feedbacks = response.data;
            } else {
              console.error('Invalid response structure:', response);
              this.feedbacks = [];
            }
          })
          .catch(error => {
            console.error('Error fetching feedbacks:', error);
            this.feedbacks = [];
          });
    },
     fetchAllDishesEvenDeleted() {
       getAllDishesEvenDeleted() // 调用 API 获取所有反馈
           .then(response => {
             if (response && response.data) {
               this.dishes = response.data;
             } else {
               console.error('Invalid response structure:', response);
               this.dishes = [];
             }
           })
           .catch(error => {
             console.error('Error fetching dishes:', error);
             this.dishes = [];
           });
    },
  }
};


</script>

<style scoped>
.modal {
  position: fixed;
  z-index: 1000;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.6);
}

.modal-content {
  background: white;
  padding: 20px;
  border-radius: 10px;
  text-align: center;
  width: 90vw;
  max-width: 900px;
  height: 80vh;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 4px 10px rgba(0,0,0,0.2);
}

.close-button {
  float: right;
  font-size: 1.5em;
  cursor: pointer;
}

.feedback-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.feedback-list {
  width: 100%;
  height: 90%;

}

.feedback-table {
  width: 100%;

  border-collapse: collapse;
}

.feedback-table th,
.feedback-table td {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: left;
}

.feedback-table th {
  background-color: #f4f4f4;
  font-weight: bold;
}

.feedback-table tr:nth-child(even) {
  background-color: #f9f9f9;
}

.feedback-content {
  white-space: pre-wrap; /* 保留换行和空格，并在需要时进行换行 */
}


h3 {
  margin-bottom: 20px;
}
</style>
